package com.tdrc.common.Interceptor;
import com.alibaba.druid.util.StringUtils;
import com.tdrc.common.annotation.JwtIgnore;
import com.tdrc.common.beans.Audience;
import com.tdrc.common.exception.CustomException;
import com.tdrc.common.util.JwtTokenUtil;
import com.tdrc.common.beans.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * @author dpf
 * @version 1.0
 * @date 2020-4-14 11:28
 * @instruction ...
 */
public class JwtInterceptor  extends HandlerInterceptorAdapter {
    private static Logger log = LoggerFactory.getLogger(JwtInterceptor.class);
    @Autowired
    private Audience audience;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 忽略带JwtIgnore注解的请求, 不做后续token认证校验
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            JwtIgnore jwtIgnore = handlerMethod.getMethodAnnotation(JwtIgnore.class);
            if (jwtIgnore != null) {
                return true;
            }
        }

        if (HttpMethod.OPTIONS.equals(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        Enumeration headerNames = request.getHeaderNames();
        // 获取请求头信息authorization信息
        final String authHeader = request.getHeader("access-token");
               // request.getHeader("authorization");
        log.info("## authHeader= {}", authHeader);

        if (StringUtils.isEmpty(authHeader)) {
            log.info("### 用户未登录，请先登录 ###");
            throw new CustomException(ResultCode.USER_NOT_LOGGED_IN);
        }

        // 获取token
        final String token = authHeader.substring(7);

        if(audience == null){
            BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
            audience = (Audience) factory.getBean("audience");
        }

        // 验证token是否有效--无效已做异常抛出，由全局异常处理后返回对应信息
        JwtTokenUtil.parseJWT(token, audience.getBase64Secret());

        return true;
    }
}
